<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Jobs\GetNewMovieJobs;
use Cache;
use Carbon\Carbon;
class GetMovieList extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'daka:get-movie-list';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新电影列表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // 推送一条 抓取列表任务
        // 获取上次采集时间
        $last_crawler_time = Cache::get('movie-list-crawler-last-time', function () {
            return Carbon::now()->subDays(1);
        });
        $now = Carbon::now();
        // 判断是否可以发起采集
        $seconds = $last_crawler_time->diffInSeconds($now);
        if ($seconds < 60) {
            $this->info('距离上次采集时间不足 1 分钟，跳过采集！');
        } else {
            $page = Cache::rememberForever('movie-list-get-page', function () {
                return 1;
            });
            if ($page >= 500) {
                // 超过500页 就跳过
                return;
            }
            dispatch(new GetNewMovieJobs($page));
            // page + 1；
            Cache::put('movie-list-get-page', ($page + 1));
            Cache::put( 'movie-list-crawler-last-time', $now);
        }

    }
}
